package com.oschina.basic.multi;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class MCycleBarrier {



	public static void main(String[] args) {
		final CyclicBarrier barrier = new CyclicBarrier(6, new Runnable() {
			@Override
			public void run() {
				System.out.println("drink bear");
			}
		});

		final int count = 7;
		for (int i = 0; i < count; i++) {
			new Thread(new Worker(i, barrier)).start();

		}
	}

}

class Worker implements Runnable {
	private final CyclicBarrier barrier;
	private final int id;

	public Worker(int id, final CyclicBarrier barrier) {
		this.id = id;
		this.barrier = barrier;
	}

	@Override
	public void run() {
		try {
			System.out.println(id + " start to run ");
			Thread.sleep(100);
			System.out.println(id + " arriaved ");
			barrier.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			e.printStackTrace();
		}

	}
}

